package com.fdd.furns.web;

import com.fdd.furns.entity.Furn;
import com.fdd.furns.entity.Page;
import com.fdd.furns.service.FurnService;
import com.fdd.furns.service.impl.FurnServiceImpl;
import com.fdd.furns.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.List;


@WebServlet(urlPatterns = "/manage/furnServlet")
public class FurnServlet extends BasicServlet {
    private FurnService furnService = new FurnServiceImpl();

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("list被调用");
        List<Furn> furns = furnService.queryFurns();
        for (Furn furn : furns) {
            System.out.println(furn);
        }
        //将furns放入request域中
        req.setAttribute("furns", furns);
        //转发到jsp
        req.getRequestDispatcher("/views/manage/furn_manage.jsp").forward(req, resp);

    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("add被调用");
//        String name = req.getParameter("name");
//        String maker = req.getParameter("maker");
//        BigDecimal price = null;
//        int sales = 0;
//        int stock = 0;
//        try {
//            price = new BigDecimal(req.getParameter("price"));
//            sales = Integer.parseInt(req.getParameter("sales"));
//            stock = Integer.parseInt(req.getParameter("stock"));
//        } catch (NumberFormatException e) {
//           req.setAttribute("msg","数据格式错误!!");
//           req.getRequestDispatcher("/views/manage/furn_add.jsp").forward(req,resp);
//           return;
//        }
//        String imgPath = req.getParameter("imgPath");
//        Furn furn = new Furn(null, name, maker, price, sales, stock, imgPath);

        //使用BeanUtils完成javabean对象的自动封装，将req对象中的参数进行复制，复制到新对象中
        Furn furn = DataUtils.copyParamToBean(req.getParameterMap(), new Furn());
        furnService.add(furn);

        //重定向到家居显示页面
        resp.setContentType("text/html;charset=utf-8");
        resp.sendRedirect(req.getContextPath() + "/manage/furnServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }

    protected void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("删除家居");
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        furnService.deleteFurn(id);

        //重定向到家居显示页面
        resp.setContentType("text/html;charset=utf-8");
        resp.sendRedirect(req.getContextPath() + "/manage/furnServlet?action=page&pageNo=" + req.getParameter("pageNo")) ;
    }

    protected void showFurn(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        System.out.println("根据id查询家居");
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        Furn furn = furnService.queryFurnById(id);
        System.out.println(furn);
        //将furn放入request
        req.setAttribute("furn", furn);


        //请求转发到修改页面，页面再去获取展示furn对象的属性
        req.getRequestDispatcher("/views/manage/furn_update.jsp").forward(req, resp);
    }


    protected void updateFurn(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        System.out.println("更新家居");

        Furn furn = DataUtils.copyParamToBean(req.getParameterMap(), new Furn());
        System.out.println(furn);
        //将furn放入request
       furnService.updateFurn(furn);


//       //重定向到家居列表页
//        resp.sendRedirect(req.getContextPath() + "/manage/furnServlet?action=list");
        resp.sendRedirect(req.getContextPath() + "/manage/furnServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }

    //获取分页数据
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        int pageNo = DataUtils.parseInt(req.getParameter("pageNo"),1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);

        //获取page对象
        Page<Furn> page = furnService.page(pageNo, pageSize);
        //将page对象放入request域中
        req.setAttribute("page",page);

        //请求转发到furn_manage.jsp
        req.getRequestDispatcher("/views/manage/furn_manage.jsp").forward(req,resp);
    }

}
